(function () {
    var defs = {};
    var instantiate = function (id) {
        var actual = defs[id];
        var dependencies = actual.deps;
        var definition = actual.defn;
        var len = dependencies.length;
        var instances = new Array(len);
        for (var i = 0; i < len; ++i)
            instances[i] = dem(dependencies[i]);
        var defResult = definition.apply(null, instances);
        if (defResult === undefined)
            throw 'module [' + id + '] returned undefined';
        actual.instance = defResult;
    };
    var def = function (id, dependencies, definition) {
        if (typeof id !== 'string')
            throw 'module id must be a string';
        else if (dependencies === undefined)
            throw 'no dependencies for ' + id;
        else if (definition === undefined)
            throw 'no definition function for ' + id;
        defs[id] = {
            deps: dependencies,
            defn: definition,
            instance: undefined
        };
    };
    var dem = function (id) {
        var actual = defs[id];
        if (actual === undefined)
            throw 'module [' + id + '] was undefined';
        else if (actual.instance === undefined)
            instantiate(id);
        return actual.instance;
    };
    var req = function (ids, callback) {
        var len = ids.length;
        var instances = new Array(len);
        for (var i = 0; i < len; ++i)
            instances[i] = dem(ids[i]);
        callback.apply(null, instances);
    };
    var ephox = {};
    ephox.bolt = {
        module: {
            api: {
                define: def,
                require: req,
                demand: dem
            }
        }
    };
    var define = def;
    var defineGlobal = function (id, ref) {
        define(id, [], function () {
            return ref;
        });
    };
    defineGlobal("global!tinymce.util.Tools.resolve", tinymce.util.Tools.resolve);
    define('tinymce.core.PluginManager', ['global!tinymce.util.Tools.resolve'], function (resolve) {
        return resolve('tinymce.PluginManager');
    });
    define('tinymce.plugins.structs.api.Commands', [], function () {
        return {
            register: function (editor) {
                editor.addCommand('structs', function () {

                });
            }
        };
    });
    define('tinymce.plugins.structs.ui.Buttons', [], function () {
        return {
            register: function (editor) {
                editor.addButton('structs', {
                    icon: 'mce-i-bullist',
                    tooltip: '文章结构',
                    cmd: 'structs'
                });
                editor.addMenuItem('structs', {
                    text: '文章结构',
                    context: 'insert',
                    icon: 'mce-i-bullist',
                    cmd: 'structs'
                });
            }
        };
    });
    define('tinymce.plugins.structs.Plugin', ['tinymce.core.PluginManager', 'tinymce.plugins.structs.api.Commands', 'tinymce.plugins.structs.ui.Buttons'], function (PluginManager, Commands, Buttons) {
        PluginManager.add('structs', function (editor) {
            Commands.register(editor);
            Buttons.register(editor);
        });
        return function () {
        };
    });
    dem('tinymce.plugins.structs.Plugin')();
})();
